package com.nuorui.module.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuorui.common.core.code.UserErrorCode;
import com.nuorui.common.core.domain.ResponseDTO;
import com.nuorui.common.utils.SmartBeanUtil;
import com.nuorui.module.system.mapper.MenuMapper;
import com.nuorui.module.system.mapper.RoleMapper;
import com.nuorui.module.system.domain.entity.MenuEntity;
import com.nuorui.module.system.domain.entity.RoleEntity;
import com.nuorui.module.system.domain.entity.RoleMenuEntity;
import com.nuorui.module.system.domain.form.RoleMenuForm;
import com.nuorui.module.system.domain.vo.MenuSimpleTreeVO;
import com.nuorui.module.system.domain.vo.MenuVO;
import com.nuorui.module.system.mapper.RoleMenuMapper;
import com.nuorui.module.system.domain.vo.RoleMenuTreeVO;
import com.nuorui.module.system.service.RoleMenuService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 用户船舶表
 *
 * @author
 * @date 2021-12-15 16:42:43
 */
@Service
@AllArgsConstructor
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenuEntity> implements RoleMenuService {

    private final RoleMapper roleMapper;
    private final MenuMapper menuMapper;

    @Override
    public ResponseDTO<RoleMenuTreeVO> getMenuTreeByRoleId(Long roleId) {
        RoleMenuTreeVO res = new RoleMenuTreeVO();
        res.setRoleId(roleId);
        //查询角色ID选择的菜单权限
        List<Long> selectedMenuId = baseMapper.selectMenuIdsByRoleId(roleId);
        res.setSelectedMenuId(selectedMenuId);
        //查询菜单权限
        List<MenuEntity> menuList = menuMapper.selectList(Wrappers.emptyWrapper());
        Map<Long, List<MenuEntity>> parentMap = menuList.stream().collect(Collectors.groupingBy(MenuEntity::getParentId, Collectors.toList()));
        List<MenuSimpleTreeVO> menuTreeList = this.buildMenuTree(parentMap, NumberUtils.LONG_ZERO);
        res.setMenuTreeList(menuTreeList);
        return ResponseDTO.ok(res);
    }

    private List<MenuSimpleTreeVO> buildMenuTree(Map<Long, List<MenuEntity>> parentMap, Long parentId) {
        // 获取本级菜单树List
        List<MenuSimpleTreeVO> res = parentMap.getOrDefault(parentId, new ArrayList<>()).stream()
                .map(e -> SmartBeanUtil.copy(e, MenuSimpleTreeVO.class)).collect(Collectors.toList());
        // 循环遍历下级菜单
        res.forEach(e -> {
            e.setChildren(this.buildMenuTree(parentMap, e.getMenuId()));
        });
        return res;
    }

    /**
     * 根据角色id集合，查询其所有的菜单权限
     */
    @Override
    public List<MenuVO> getMenusByRoleIdList(List<Long> roleIdList, boolean isAdministrator) {
        //管理员返回所有菜单
        if (isAdministrator) {
            List<MenuEntity> menuEntityList = baseMapper.selectByRoleIdList(new ArrayList<>());
            return SmartBeanUtil.copyList(menuEntityList, MenuVO.class);
        }
        //非管理员 无角色 返回空菜单
        if (CollectionUtil.isEmpty(roleIdList)) {
            return new ArrayList<>();
        }
        List<MenuEntity> menuEntityList = baseMapper.selectByRoleIdList(roleIdList);
        return SmartBeanUtil.copyList(menuEntityList, MenuVO.class);
    }

    @Override
    public ResponseDTO updateRoleMenu(Long roleId, RoleMenuForm roleMenuForm) {
        //查询角色是否存在
        RoleEntity roleEntity = roleMapper.selectById(roleId);
        if (null == roleEntity) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }
        List<RoleMenuEntity> roleMenuList = new ArrayList();
        List<Long> selectedMenuIdList = roleMenuForm.getMenuIdList();
        for (Long menuId : selectedMenuIdList) {
            RoleMenuEntity roleMenuEntity = new RoleMenuEntity(roleId, menuId);
            roleMenuList.add(roleMenuEntity);
        }
        // 根据角色ID删除菜单权限
        baseMapper.deleteByRoleId(roleId);
        // 批量添加菜单权限
        saveBatch(roleMenuList);

        return ResponseDTO.ok();
    }

}
